var http = require("http");
var url = require("url");

function start(route, handle){
	function onRequest(request,response){
		// var pathname = url.parse(request.url).pathname;
		// var para = url.parse(request.url).query; 

		// console.log("Request for "+pathname+" received.");
		// console.log("para: "+ para);
		

		// response.writeHead(200,{"Content-Type":"text/plain"});
		// var content = route(pathname,handle);
		// response.write(content);
		// response.end();

		// route(pathname, handle,  response);

		var postData = "";
		var pathname = url.parse(request.url).pathname;
		console.log("--Request for "+ pathname + " received.");
		request.setEncoding("utf8");
		request.addListener("data",function(postDataChunk){
			postData += postDataChunk;
			console.log("Received Post data chunk "+postDataChunk);
		});
		request.addListener("end",function(){
			route(pathname, handle,  response, postData);
		});
	}
	http.createServer(onRequest).listen(8888);
	console.log("Server has started.\n");
}


exports.start = start;